package com.shujia.mr.dianxin2;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.ArrayList;

public class DianXinJoinCityReducer extends Reducer<Text, Text, Text, Text> {
    @Override
    protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
        //<"83401",["$00595A38A1F98846E4F788276F34173EC758BC20-22","#合肥市","$47BE1E866CFC071DB19D5E1C056BE28AE24C16E7-11"]>
        String city_name = null;

        //创建一个集合存储一个城市中的多个用户
        ArrayList<String> phoneList = new ArrayList<>();

        for (Text value : values) {
            String info = value.toString();
            if (info.startsWith("$")) {
                //添加到集合中
                phoneList.add(info.substring(1)); //去掉$符号
//                String[] strings = info.split("-");
//                phoneNum = strings[0].substring(1);
//                avg_time = strings[1];
            } else if (info.startsWith("#")) {
                city_name = info.substring(1);
            }
        }

        //循环遍历该城市的每个用户
        for (String userInfo : phoneList) {
            //47BE1E866CFC071DB19D5E1C056BE28AE24C16E7-11
            String[] strings = userInfo.split("-");
            String phoneNum = strings[0];
            String avg_time = strings[1];
            String new_key = phoneNum + "-" + city_name;
            context.write(new Text(new_key), new Text(avg_time));
        }
    }
}
